﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Model;
using DAL;
using BAL;

namespace blog
{
    public partial class Login : System.Web.UI.Page
    {
        bool ischecked;
        private User user;
        private string name;
        private string pass;


        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        protected void submit_Click(object sender, EventArgs e)
        {
            ischecked = this.isremember.Checked;
            name=this.username.Text.ToString().Trim();
            pass=this.password.Text.ToString().Trim();
            user = new Model.User(name, pass);
            
            BAL.DoUser BalUser = new BAL.DoUser();
            bool ispass = BalUser.checkloglin(name, pass);
            if (ispass)
            {
                if (ischecked)
                {
                    HttpCookie usercookie = new HttpCookie("user");
                    usercookie.Values["username"] = name;
                    usercookie.Values["password"] = pass;
                    usercookie.Expires = DateTime.Now.AddDays(2);
                    Response.Cookies.Add(usercookie);
                    Response.Redirect("Index.aspx");

                }
                else
                {
                    Session["username"] = name;
                    Session["password"] = pass;
                    Response.Redirect("Index.aspx");
                }
            }
            else
            {
                Alert(this.Page,"密码错误!");

            }
        }

        public static void Alert(System.Web.UI.Page page, string msg)
        {
            page.ClientScript.RegisterStartupScript(page.GetType(), "message", "<script language='javascript' defer>alert('" + msg.ToString() + "');</script>");
        }
    }
}